// XMLLib version 1
//
// Author
//
//   Thomas Koos (info@muetze1.de)
//   www.muetze1.de
//
// License
//
//   This Source Code Form is subject to the terms of the Mozilla Public
//   License, v. 1.1. If a copy of the MPL was not distributed with this
//   file, You can obtain one at http://mozilla.org/MPL/1.1/.
//
// Description
//
//   ISO-8859-1 charset coding unit
//
// Revision
//
//  $Id: StrAdapterISO8859_1.pas 18 2014-04-21 21:28:10Z muetze1@gmail.com $
//

Unit StrAdapterISO8859_1;

Interface

{$Z+}  // equal to $Z4 on newer compilers

Uses
  StrAdapterOptions,       // for TSAEntityCoding
  StrAdapterBase;          // for TBaseStringAdapter

    //## "ISO-8859-1"
Type
  TISO8859_1Adapter = class(TBaseStringAdapter)
  public
    class function EncodingID: string; override;

      // Abstract base function for encoding the passed ansi string to the
      // given charset.
    function EncodeEntity(out ADestination: string; const ACode: integer; const AFormat: TSAEntityCoding): boolean; override;

      // Abstract base function for decoding the passed string in the
      // given charset to return the decoded ansi string.
    function DecodeEntity(var ADestination: PChar; const ACodeData: string): boolean; override;

    function Decode(Const AData: AnsiString): String; Override;
    function Encode(Const AData: String): AnsiString; Override;
  end;

implementation

{ TISO8859_1Adapter }

uses
  StrAdapter,
  StrAdapterEntity,
  SysUtils;

function TISO8859_1Adapter.Decode(const AData: AnsiString): string;
begin
  result := string(AData);
end;

function TISO8859_1Adapter.DecodeEntity(var ADestination: PChar; const ACodeData: string): boolean;
var
  lCode, i: Integer;
begin
  lCode := 0;
  for I := low(coExtendedEntityTable) to high(coExtendedEntityTable) do
  begin
    if coExtendedEntityTable[i].Name = ACodeData then
    begin
      lCode := coExtendedEntityTable[i].Unicode;
      break;
    end;
  end;

    // valid entity?
  if lCode > 0 then
    result := DecodeEntity(ADestination, lCode)
  else
    result := inherited DecodeEntity(ADestination, ACodeData);
end;

function TISO8859_1Adapter.Encode(const AData: string): AnsiString;
begin
  result := AnsiString(AData);
end;

function TISO8859_1Adapter.EncodeEntity(out ADestination: string; const ACode: integer; const AFormat: TSAEntityCoding): boolean;
var
  lFound: Boolean;
  I: Integer;
begin
  result := true;
  lFound := false;

  if AFormat = saecNamed then
  begin
    for I := low(coExtendedEntityTable) to high(coExtendedEntityTable) do
    begin
      if ACode = coExtendedEntityTable[i].Unicode then
      begin
        ADestination := '&' + coExtendedEntityTable[i].Name + ';';

        lFound := true;
        break;
      end;
    end;
  end;

  if not lFound then
    result := inherited EncodeEntity(ADestination, ACode, AFormat);
end;

class function TISO8859_1Adapter.EncodingID: string;
begin
  result := 'ISO-8859-1';
end;

initialization
  TStringAdapter.RegisterAdapter(TISO8859_1Adapter);

end.


